#! /usr/bin/env python
# -*- coding: utf-8 -*-


class Solution(object):

    # @param nestedList a list, each element in the list
    # can be a list or integer, for example [1,2,[1,2]]
    # @return {int[]} a list of integer
    def flatten1(self, nestedList):
        be_flatten = [nestedList]
        results = []

        while be_flatten:
            top = be_flatten.pop()

            if type(top) is not list:
                results.append(top)
            else:
                be_flatten.extend( reversed(top) )

        return results

    def flatten2(self, nestedList):
        if type(nestedList) is not list:
            return [nestedList]

        results = []
        for ele in nestedList:
            # print(ele)
            results.extend(self.flatten(ele))

        return results

    def flatten3(self, nestedList):
        if not nestedList:
            return []

        # 输入可能是整数
        if type(nestedList) is not list:
            return [nestedList]

        top = nestedList[0]

        # print(top)
        # print(nestedList[1:])
        # print('===================  =====================')

        if type(top) is not list:
            # int, float
            return [top] + self.flatten(nestedList[1:])
        else:
            return self.flatten(top) + self.flatten(nestedList[1:])

    flatten = flatten3


def main():
    print("start main")
    s = Solution()
    r = s.flatten([4,[3,[2,[1]]]])
    # r = s.flatten([1, [2, 3]])
    # r = s.flatten([2, 3])
    print(r)

if __name__ == "__main__":
    main()
